
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>react/商品详情页滚动监听与点击跳转 - 初一蛙鸣</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="初一蛙鸣-前端-小程序-vue-react,效果图
tsx12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505,"> 
    
    <link rel="alternative" href="atom.xml" title="初一蛙鸣" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="react/商品详情页滚动监听与点击跳转 - 初一蛙鸣"/>
    <meta name="twitter:description" content="初一蛙鸣-前端-小程序-vue-react,效果图
tsx12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505,"/>
    
    
    
    
    <meta property="og:site_name" content="初一蛙鸣"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="react/商品详情页滚动监听与点击跳转 - 初一蛙鸣"/>
    <meta property="og:description" content="初一蛙鸣-前端-小程序-vue-react,效果图
tsx12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 4.2.1"></head>

<body class="loading">
    <span id="config-title" style="display:none">初一蛙鸣</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://lhl946.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">react/商品详情页滚动监听与点击跳转</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">react/商品详情页滚动监听与点击跳转</h1>
        <div class="stuff">
            <span>七月 10, 2020</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/react/" rel="tag">react</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/%E5%95%86%E5%9F%8E/" rel="tag">商城</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F/" rel="tag">小程序</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/%E6%BB%9A%E5%8A%A8%E7%9B%91%E5%90%AC/" rel="tag">滚动监听</a></li></ul>


        </div>
        <div class="content markdown">
            <h3 id="效果图"><a href="#效果图" class="headerlink" title="效果图"></a>效果图</h3><p><img src="/static/images/202007101125.gif" alt=""></p>
<h3 id="tsx"><a href="#tsx" class="headerlink" title="tsx"></a>tsx</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line">import Taro, &#123; Config &#125; from &quot;@tarojs&#x2F;taro&quot;</span><br><span class="line">import &#123; View, Swiper, SwiperItem, ScrollView &#125; from &quot;@tarojs&#x2F;components&quot;</span><br><span class="line"></span><br><span class="line">import cs from &quot;classnames&quot;</span><br><span class="line">import &quot;.&#x2F;test.less&quot;</span><br><span class="line"></span><br><span class="line">interface State &#123;</span><br><span class="line">  tab: number</span><br><span class="line">  spit: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default class Test extends Taro.Component&lt;&#123;&#125;, State&gt; &#123;</span><br><span class="line">  config: Config &#x3D; &#123;</span><br><span class="line">    navigationBarTitleText: &quot;商品详情&quot;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  state: State &#x3D; &#123;</span><br><span class="line">    tab: 0,</span><br><span class="line">    spit: 0</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  componentDidMount() &#123;</span><br><span class="line">    this.getSpitTop()</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 监听页面滚动</span><br><span class="line">  onPageScroll(e: Taro.PageScrollObject) &#123;</span><br><span class="line">    if (e.scrollTop &lt; this.state.spit) &#123;</span><br><span class="line">      this.setState(&#123;</span><br><span class="line">        tab: 0</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      this.setState(&#123;</span><br><span class="line">        tab: 1</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &#x2F;&#x2F; 获取分割线的位置</span><br><span class="line">  getSpitTop() &#123;</span><br><span class="line">    let query &#x3D; Taro.createSelectorQuery()</span><br><span class="line">    query.select(&quot;#spit&quot;).boundingClientRect()</span><br><span class="line">    query.selectViewport().scrollOffset()</span><br><span class="line">    query.exec(res &#x3D;&gt; &#123;</span><br><span class="line">      this.setState(&#123;</span><br><span class="line">        spit: res[0].top</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  render() &#123;</span><br><span class="line">    return (</span><br><span class="line">      &lt;View className&#x3D;&quot;goods&quot;&gt;</span><br><span class="line">        &lt;View className&#x3D;&quot;header&quot;&gt;</span><br><span class="line">          &lt;View</span><br><span class="line">            onClick&#x3D;&#123;() &#x3D;&gt; &#123;</span><br><span class="line">              this.setState(&#123;</span><br><span class="line">                tab: 0</span><br><span class="line">              &#125;)</span><br><span class="line">              Taro.pageScrollTo(&#123;</span><br><span class="line">                scrollTop: 0</span><br><span class="line">              &#125;)</span><br><span class="line">            &#125;&#125;</span><br><span class="line">            className&#x3D;&#123;cs(this.state.tab &#x3D;&#x3D;&#x3D; 0 ? &quot;active&quot; : &quot;&quot;, &quot;item&quot;)&#125;</span><br><span class="line">          &gt;</span><br><span class="line">            商品</span><br><span class="line">          &lt;&#x2F;View&gt;</span><br><span class="line">          &lt;View</span><br><span class="line">            onClick&#x3D;&#123;() &#x3D;&gt; &#123;</span><br><span class="line">              this.setState(&#123;</span><br><span class="line">                tab: 1</span><br><span class="line">              &#125;)</span><br><span class="line">              Taro.pageScrollTo(&#123;</span><br><span class="line">                selector: &quot;#spit&quot;</span><br><span class="line">              &#125;)</span><br><span class="line">            &#125;&#125;</span><br><span class="line">            className&#x3D;&#123;cs(this.state.tab &#x3D;&#x3D;&#x3D; 1 ? &quot;active&quot; : &quot;&quot;, &quot;item&quot;)&#125;</span><br><span class="line">          &gt;</span><br><span class="line">            详情</span><br><span class="line">          &lt;&#x2F;View&gt;</span><br><span class="line">        &lt;&#x2F;View&gt;</span><br><span class="line">        &lt;View className&#x3D;&quot;container&quot;&gt;</span><br><span class="line">          &lt;Swiper className&#x3D;&quot;list&quot; id&#x3D;&quot;goods&quot;&gt;</span><br><span class="line">            &lt;SwiperItem className&#x3D;&quot;list-item&quot;&gt;1&lt;&#x2F;SwiperItem&gt;</span><br><span class="line">            &lt;SwiperItem className&#x3D;&quot;list-item&quot;&gt;2&lt;&#x2F;SwiperItem&gt;</span><br><span class="line">            &lt;SwiperItem className&#x3D;&quot;list-item&quot;&gt;3&lt;&#x2F;SwiperItem&gt;</span><br><span class="line">          &lt;&#x2F;Swiper&gt;</span><br><span class="line">          &lt;View className&#x3D;&quot;other&quot;&gt;item&lt;&#x2F;View&gt;</span><br><span class="line">          &lt;View className&#x3D;&quot;other&quot;&gt;item&lt;&#x2F;View&gt;</span><br><span class="line">          &lt;View id&#x3D;&quot;spit&quot;&gt;&lt;&#x2F;View&gt;</span><br><span class="line">          &lt;View className&#x3D;&quot;detail&quot;&gt;</span><br><span class="line">            &lt;View className&#x3D;&quot;detail-item&quot;&gt;详情项&lt;&#x2F;View&gt;</span><br><span class="line">            &lt;View className&#x3D;&quot;detail-item&quot;&gt;详情项&lt;&#x2F;View&gt;</span><br><span class="line">            &lt;View className&#x3D;&quot;detail-item&quot;&gt;详情项&lt;&#x2F;View&gt;</span><br><span class="line">            &lt;View className&#x3D;&quot;detail-item&quot;&gt;详情项&lt;&#x2F;View&gt;</span><br><span class="line">          &lt;&#x2F;View&gt;</span><br><span class="line">        &lt;&#x2F;View&gt;</span><br><span class="line">      &lt;&#x2F;View&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="less"><a href="#less" class="headerlink" title="less"></a>less</h3><figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="comment">// height: 20px;</span></span><br><span class="line">  <span class="comment">// overflow: hidden;</span></span><br><span class="line">  <span class="comment">// transition: 2s;</span></span><br><span class="line"></span><br><span class="line">  <span class="selector-tag">&amp;</span><span class="selector-class">.show</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="selector-class">.counter</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">0</span> auto;</span><br><span class="line">    <span class="attribute">display</span>: flex;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 商品详情页滚动相关的</span></span><br><span class="line"><span class="selector-class">.goods</span> &#123;</span><br><span class="line">  <span class="attribute">display</span>: flex;</span><br><span class="line">  <span class="attribute">flex-direction</span>: column;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100vh</span>;</span><br><span class="line"></span><br><span class="line">  <span class="selector-class">.container</span> &#123;</span><br><span class="line">    <span class="attribute">flex</span>: <span class="number">1</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="selector-class">.header</span> &#123;</span><br><span class="line">    <span class="attribute">display</span>: flex;</span><br><span class="line">    <span class="attribute">justify-content</span>: space-around;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">44px</span>;</span><br><span class="line">    <span class="attribute">align-items</span>: center;</span><br><span class="line">    <span class="attribute">font-size</span>: <span class="number">18px</span>;</span><br><span class="line">    <span class="attribute">font-family</span>: PingFangSC-Regular, PingFang SC;</span><br><span class="line">    <span class="attribute">font-weight</span>: <span class="number">400</span>;</span><br><span class="line">    <span class="attribute">color</span>: rgba(<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">1</span>);</span><br><span class="line">    <span class="attribute">line-height</span>: <span class="number">44px</span>;</span><br><span class="line">    <span class="attribute">border-bottom</span>: <span class="number">1px</span> solid <span class="number">#eeeeee</span>;</span><br><span class="line"></span><br><span class="line">    <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">    <span class="attribute">position</span>: fixed;</span><br><span class="line"></span><br><span class="line">    <span class="selector-class">.item</span> &#123;</span><br><span class="line">      <span class="attribute">width</span>: <span class="number">72px</span>;</span><br><span class="line">      <span class="attribute">text-align</span>: center;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="selector-class">.active</span> &#123;</span><br><span class="line">      <span class="attribute">color</span>: <span class="number">#1677ff</span>;</span><br><span class="line">      <span class="attribute">border-bottom</span>: <span class="number">2px</span> solid <span class="number">#1677ff</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="selector-class">.list</span> &#123;</span><br><span class="line">    <span class="attribute">margin-top</span>: <span class="number">44px</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="selector-class">.list-item</span> &#123;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#eeeeee</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="selector-class">.other</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">375px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">2px</span> solid salmon;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="selector-class">.detail-item</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">375px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">2px</span> solid <span class="number">#1677ff</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
